<?php
//////////////////////////////////////
// Class Registration System		//
// CPSC 462					      	//
// Course Directory Page			//
// Trent Robbins & Richard Heywood  //
//////////////////////////////////////

date_default_timezone_set('America/New_York');
include('Website.class.php');
$css = '';
$js = '';
$js .= "<script type='text/javascript'>".file_get_contents('./js/main.js')."</script>";
$page = new Website();
$page->dbConnect();
$studId = $_GET['studId'];

$body = '<div id="0">';

/*If nothing is set. Display courses by subject*/
if(!isset($_GET['subject']) && (!isset($_GET['courseNum']) || !is_numeric($_GET['courseNum']))){

	$body = 'Please Select a Course Subject:<br /><br />';
			/*MySQL query to get all of the course subjects*/	
			$subjectList = $page->getSubjects();
			foreach ($subjectList as $subject){
				$body .= "<a href='./courseDirectory.php?studId=$studId&subject=$subject'> $subject -- ".$page->getSubjectName($subject)." </a><br />";
			}
}
/*If a subject has been selected,display classes by course number*/
elseif(isset($_GET['subject']) && (!isset($_GET['courseNum']) || !is_numeric($_GET['courseNum']))){
	$body .= 'Please Select a Course Number: <br />';	
			$ndx = 0;
			/*MySQL query here to get all of the course subjects with their description and hours*/
			list($courseList,$descriptionList,$hoursList) = $page->getCourseInfoBySubject($_GET['subject']);
			foreach ($courseList as $courseNum){
				$description = $descriptionList[$ndx];
				$courseHours = $hoursList[$ndx];
				$body .= "<a href='./courseDirectory.php?studId={$studId}&subject={$_GET['subject']}&courseNum={$courseNum}'> 
							{$_GET['subject']} {$courseNum} -- {$description} -- {$courseHours} hours </a><br />";
				$ndx++;
			}
}
/*If a course number and subject have been selected, display availabe sections and course info*/
elseif(isset($_GET['subject']) && isset($_GET['courseNum']) && is_numeric($_GET['courseNum'])){
	$sections = array();
	$body .= 'Please Select a Section:<br /><br />';
	/*MySQL query to generate a list of tuples of sections for the given subject and course number*/
	$sections = $page->getSections($_GET['subject'],$_GET['courseNum']);
	$body .= $page->makeCourseTable($sections,$studId);			 
}
else{
	$body .= "Invalid Selection";
}

$body .= '<br /><br />';
$body .= "<a href='./mainPage.php?studId=$studId'><input type='button' value='Back to My Courses' id='cdBackButton' /></a>";
$body .= '</div>';

$page->createPage('Course Directory',$css,$js,$body);
$page->dbCloseConnect();

?>